package com.xinfu.newproject.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.xinfu.newproject.R;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.ui.household_applications.HouseholdFourFragment;
import com.xinfu.newproject.ui.household_applications.HouseholdOneFragment;
import com.xinfu.newproject.ui.household_applications.HouseholdThreeFragment;
import com.xinfu.newproject.ui.household_applications.HouseholdTwoFragment;
import com.xinfu.newproject.adapter.TicketPagerAdapter;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HouseholdApplicationsActivity extends BaseActivity {

    @BindView(R.id.activity_household_applications_magicIndicator)
    MagicIndicator mIndicator;

    @BindView(R.id.activity_household_applications_vp)
    ViewPager mViewPager;

    private static final String[] CHANNELS = new String[]{"业主信息", "购房合同", "车辆信息", "其他附件"};
    private List<String> mDataList = Arrays.asList(CHANNELS);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_household_applications);
        ButterKnife.bind(this);
        registerBack();
        setActivityTitle("入户申请");
        initViewBind();
        initIndicator();
        initLisener();
    }

    private void initLisener() {

    }

    private void initViewBind() {
        ArrayList<Fragment> mFraList = new ArrayList<>();
        HouseholdOneFragment fragment0 = new HouseholdOneFragment();
        HouseholdTwoFragment fragment1 = new HouseholdTwoFragment();
        HouseholdThreeFragment fragment2 = new HouseholdThreeFragment();
        HouseholdFourFragment fragment3 = new HouseholdFourFragment();

        mFraList.add(fragment0);
        mFraList.add(fragment1);
        mFraList.add(fragment2);
        mFraList.add(fragment3);

        TicketPagerAdapter pagerAdapter = new TicketPagerAdapter(getSupportFragmentManager(), mFraList);
        mViewPager.setOffscreenPageLimit(4);
        mViewPager.setAdapter(pagerAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    private void initIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(this);
        //自适应tab的个数居中,
        // 布局中让在xml里中将MagicIndicator套在Gravity为Center的布局中
        commonNavigator.setAdjustMode(true);

        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));

                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.gray_6));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.yellow_ff));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.yellow_ff));
                return indicator;
            }
        });
        mIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerPadding(UIUtil.dip2px(this, 15));
        titleContainer.setDividerDrawable(getResources().getDrawable(R.drawable.simple_splitter));
        ViewPagerHelper.bind(mIndicator, mViewPager);
    }

    //解决重叠，方法1
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //如果用以下这种做法则不保存状态，再次进来的话会显示默认的tab
        super.onSaveInstanceState(outState);
    }
}
